package onlinejudge.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import onlinejudge.constans.Constant;
import onlinejudge.entity.Comment;
import onlinejudge.entity.Reply;
import onlinejudge.entity.Solution;
import onlinejudge.service.ProblemService;
import onlinejudge.service.SolutionCommentService;
import onlinejudge.service.SolutionService;
import onlinejudge.vo.SolutionVo;

@Controller
@RequestMapping("/solution")
public class SolutionController {

	@Resource
	private SolutionService solutionService;
	@Resource
	private ProblemService problemService;
	@Resource
	private SolutionCommentService solutionCommentService;
	
	/**
	 * 题解列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		Integer offset=Integer.parseInt(request.getParameter("pager.offset"));
		String queryContent=request.getParameter("queryContent");
		if(queryContent!=null&&queryContent.equals("")) {
			queryContent=null;
		}
		Map<String, Object> parameters=new HashMap<String, Object>();
		parameters.put("queryContent",queryContent);
		//再把搜索内容放回页面上,这样保证了对搜索结果的分页
		request.setAttribute("queryContent", queryContent);
		//在未分页之前先获取搜索结果的数量,也便于对搜索结果的分页
		int resSize=solutionService.querySolution(parameters).size();
		//分页
		parameters.put("page",offset);
		parameters.put("limit",Constant.getRows());
		List<SolutionVo> solutionList=solutionService.querySolution(parameters);
		
		request.setAttribute("solutionList", solutionList);
		request.setAttribute("total", resSize);
		request.setAttribute("url", "solution/list");
		return "client/solution";
	}
	/**
	 * 访问每个题解
	 * @param solutionId
	 * @param request
	 * @return
	 */
	@RequestMapping("/item")
	public String item(Integer solutionId,HttpServletRequest request) {
		request.setAttribute("solution", solutionService.getSolution(solutionId));
		request.setAttribute("commentList", solutionCommentService.queryAllComment(solutionId));
		request.setAttribute("commentCount", solutionCommentService.getCommentCount(solutionId));
		return "client/solutionItem";
	}
	/**
	 * 写题解页面导向
	 * @param problemId
	 * @param request
	 * @return
	 */
	@RequestMapping("/writeSolution")
	public String writeSolution(Integer problemId,HttpServletRequest request) {
		request.setAttribute("problem", problemService.getProblem(problemId));
		return "client/writeSolution";
	}
	/**
	 * 写题解
	 * @param solution
	 * @return
	 */
	@RequestMapping("/addSolution")
	public String addSolution(Solution solution) {
		solution.setSolutionContent(solution.getSolutionContent().replace("<br />", "<div style='margin-top:10px;'></div>"));
		solution.setCode(solution.getCode().replace("<br />", ""));
		solutionService.addSolution(solution);
		return "redirect:/solution/list?pager.offset=0";
	}
	/**
	 * 更新题解阅读量
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateViewCount")
	public String updateViewCount(@RequestBody Map<String,Object> map) {
		Solution solution=new Solution();
		solution.setSolutionId((Integer) map.get("solutionId"));
		solution.setSolutionViewCount(((Integer) map.get("solutionViewCount"))+1);
		solutionService.updateViewCount(solution);
		JSONObject json=new JSONObject();
		json.put("solutionViewCount",solution.getSolutionViewCount());
		return json.toJSONString();
	}
	/**
	 * 添加题解的评论接口
	 * @param comment
	 * @param solutionId
	 * @return
	 */
	@RequestMapping("/addComment")
	public String addComment(Comment comment,Integer solutionId) {
		solutionCommentService.addComment(comment, solutionId);
		return "redirect:/solution/item?solutionId="+solutionId;
	}
	/**
	 * 添加题解评论的回复
	 * @param reply
	 * @param solutionId
	 * @return
	 */
	@RequestMapping("/addReply")
	public String addReply(Reply reply,Integer solutionId) {
		solutionCommentService.addReply(reply);
		return "redirect:/solution/item?solutionId="+solutionId;
	}
}
